home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS17.ADF / C / FixObj.C < prev    next >
C/C++ Source or Header  |  1989-01-27  |  2KB  |  75 lines

  1. /* FIXOBJ : cleanup routine for Amiga Object files (c) 1986 John Hodgson
  2.  
  3.    Note : FIXOBJ attempts to remove last record filler by truncating all
  4.    information past the last HUNK_END, providing the HUNK_END sequence
  5.    is within the last 128-byte record. The algorithm used here could be
  6.    thwarted if end-of-file filler happens to contain a bogus HUNK_END, or
  7.    if a particular load file ends with anything besides HUNK_END.
  8.  
  9.    Format: FIXOBJ <source-file> <dest-file>.
  10.  
  11.    Note : Due to a bug in the RAM: device, do not attempt to FIXOBJ
  12.           the source file if residing in ramdisk. (as of 1.1)
  13.  
  14.    Rev 1.1 : Corrected for use with Aztec "C" */
  15.  
  16. #include <stdio.h>
  17.  
  18. /* constant definitions */
  19.  
  20. #define REVHUNK_END (0xf203)
  21. #define BYTEBITS (8)
  22. #define ENDOFFSET (2)
  23.  
  24. main(argc,argv)
  25. int argc;
  26. char *argv[];
  27. {
  28.   FILE *rdptr,*wrtptr,*fopen();
  29.   long pos,ctr,ftell();
  30.   unsigned short media;
  31.  
  32.   if (argc!=3) {
  33.     printf("Usage : fixobj srcfile destfile\n");
  34.     exit(0);
  35.   }
  36.   if ((rdptr=fopen(argv[1],"r"))==NULL) {
  37.     printf("Error opening file %s for reading.\n",argv[1]);
  38.     exit(0);
  39.   }
  40.  
  41. /* search last record backwards for HUNK_END combo in reverse order */
  42.  
  43.   media=0;
  44.   for (ctr=-1;ctr>-128;ctr--) {
  45.  
  46.     /* simulate a 16-bit shift register for easy hunk check */
  47.  
  48.     media<<=BYTEBITS;
  49.     fseek(rdptr,ctr,ENDOFFSET);
  50.     media|=getc(rdptr);
  51.     if (media == REVHUNK_END) break;
  52.   }
  53.   if ((media!=REVHUNK_END) || ctr==-2) {
  54.     (ctr==-2) ? printf("No corrections made!\n"):
  55.                 printf("Invalid load format!\n");
  56.     fclose(rdptr);
  57.     exit(0);
  58.   }
  59.   if ((wrtptr=fopen(argv[2],"w"))==NULL) {
  60.     printf("Error opening file %s for writing.\n",argv[2]);
  61.     fclose(rdptr);
  62.     exit(0);
  63.   }
  64.  
  65. /* duplicate file up to HUNK_END, inclusive */
  66.  
  67.   pos=ftell(rdptr)+1;
  68.   rewind(rdptr);
  69.  
  70.   for (ctr=0;ctr<pos;ctr++) putc(getc(rdptr),wrtptr);
  71.  
  72.   fclose(rdptr); fclose(wrtptr);
  73. }
  74.  
  75.